package com.wanchuang.hepos.ui.page.home;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.google.protobuf.InvalidProtocolBufferException;
import com.wanchuang.hepos.R;
import com.wanchuang.hepos.bridge.data.repository.Apis;
import com.wanchuang.hepos.bridge.data.repository.WebApis;
import com.wanchuang.hepos.bridge.request.MineRequestViewModel;
import com.wanchuang.hepos.bridge.state.home.TiXianViewModel;
import com.wanchuang.hepos.databinding.FragmentTiXianBinding;
import com.wanchuang.hepos.net.NetworkManager;
import com.wanchuang.hepos.net.interfaces.OnRequestCallBack;
import com.wanchuang.hepos.proto.Settle;
import com.wanchuang.hepos.ui.base.BaseFragment;
import com.wanchuang.hepos.ui.web.ui.WebViewActivity;

/**
 * 文件名 : HomeRootFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/24 16:20
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */
public class TiXianFragment extends BaseFragment {
    private FragmentTiXianBinding mBinding;
    private TiXianViewModel mViewModel;
    private MineRequestViewModel mRequestViewModel;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(TiXianViewModel.class);
        mRequestViewModel = ViewModelProviders.of(this).get(MineRequestViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_ti_xian, container, false);
        mBinding = FragmentTiXianBinding.bind(view);
        mBinding.setClick(new ClickProxy());
        mBinding.setVm(mViewModel);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRequestViewModel.getErrorString().observe(this, s -> {
            showShortToast(s);
        });

        mRequestViewModel.getResultString().observe(this, s -> {
            showShortToast(s);
            nav().navigateUp();
        });




        NetworkManager.INSTANCE.post(Apis.get_wallet, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) throws InvalidProtocolBufferException {
                Settle.get_wallet  getWallet = Settle.get_wallet.parseFrom(bytes);
                mViewModel.wallet_d0.set("￥"+getWallet.getWalletD0());
                mViewModel.wallet_t1.set("￥"+getWallet.getWalletT1());
                mViewModel.wallet_total.set("￥"+getWallet.getWalletTotal());
                mViewModel.wallet_temp.set("￥"+getWallet.getWalletTemp());
//                mViewModel.wallet_temp.set("￥"+"1000");
            }

            @Override
            public void onError(int code, String errorMessage) {
            }
        });


    }


    public class ClickProxy {
        public void back() {
            nav().navigateUp();
        }
        public void mingxione() {
            WebViewActivity.startActivity(mActivity, WebApis.accountList +"?wallet_type=1");
        }
        public void mingxitwo() {
            WebViewActivity.startActivity(mActivity, WebApis.accountList +"?wallet_type=2");
        }
        public void mingxithree() {
            WebViewActivity.startActivity(mActivity, WebApis.accountList +"?wallet_type=3");
        }
        public void tixian() {
            nav().navigate(R.id.action_tiXianFragment_to_tiXianClickFragment);
        }

        public void submit() {
            if (TextUtils.isEmpty(mViewModel.bean.get())) {
                showShortToast("请输入意见反馈内容");
                return;
            }
            mRequestViewModel.feedback_add(mViewModel.bean.get());
        }
    }
}
